﻿using AppFramework.Demo.Dtos;
using AppFramework.Demo;
using AppFramework.Shared;
using AppFramework.Shared.Services;
using Prism.Regions;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Threading;
using System.Windows.Controls;
using System.Windows.Markup;
using AppFramework.DinosaursParkMes.DataBoard;
using AppFramework;
using AppFramework.DinosaursParkMes.Execute.Dto;
using AppFramework.DinosaursParkMes.Execute;
using AppFramework.DinosaursParkMes.DataBoard.Dto;
using System.Linq;

namespace ViewModels.DinosaursParkMes.DataBoards
{
    public class BoardViewModel: NavigationCurdViewModel
    {
        IDataBoardService dataBoard;
        private readonly IExecuteScheduleAppService appService;
        private readonly ExecuteDto executeDto;
        List<ReportOfWorkerOutputDTO> reportOfWorkerdto;
        public BoardViewModel(IDataBoardService dataBoard, IExecuteScheduleAppService appService, ExecuteDto executeDto = null, List<ReportOfWorkerOutputDTO> reportOfWorkerdto = null)
        {
            Title = "数据看板";
            //定义时间
            var priority = new DispatcherPriority();
            priority = DispatcherPriority.Send;//设置优先级为最高（按需）
            DispatcherTimer timer = new DispatcherTimer(priority);
            timer.Interval = new TimeSpan(0, 0, 0, 1);
            timer.Tick += timer_Tick;
            timer.IsEnabled = true;
            this.dataBoard = dataBoard;
            this.appService = appService;
            this.executeDto = executeDto;
            this.reportOfWorkerdto = new List<ReportOfWorkerOutputDTO>();
        }

        private int _OrderCount { get; set; }
        private int _WorkCount { get; set; }
        private int _ProductCount { get; set; }
        public int OrderCount { get { return _OrderCount; } set { _OrderCount = value; OnPropertyChanged(); } }
        public int WorkCount { get { return _WorkCount; } set { _WorkCount = value; OnPropertyChanged(); } }
        public int ProductCount { get { return _ProductCount; } set { _ProductCount = value; OnPropertyChanged(); } }
        #region 时间
        /*当前日期*/
        private string _DateNow;
        public string DateNow
        {
            get { return _DateNow; }
            set { _DateNow = value; OnPropertyChanged(); }
        }
        /*当前时间*/
        private string _TimeNow;
        public string TimeNow
        {
            get { return _TimeNow; }
            set { _TimeNow = value; OnPropertyChanged(); }
        }
        /*事件处理器*/
        private void timer_Tick(object sender, EventArgs e)
        {
            DateNow = DateTime.Now.ToString("yyyy-MM-dd ");
            TimeNow = DateTime.Now.ToString("HH:mm:ss");
        }
        #endregion

        /// <summary>
        /// 进度列表
        /// </summary>
        /// <returns></returns>
        private async Task GetExecutes()
        {
            await SetBusyAsync(async () =>
            {
                await appService.GetExecutes().WebAsync(dataPager.SetList);
            });
        }
        public List<ReportOfWorkerOutputDTO> ReportOfWorkerOutputDTO
        {
            get { return reportOfWorkerdto; }
            set { reportOfWorkerdto=value;  OnPropertyChanged(); }
        }
        /// <summary>
        /// 产品列表
        /// </summary>
        /// <param name=""></param>
        /// <returns></returns>
        private async Task GetProductList() {

            await SetBusyAsync(async () =>
            {
                ReportOfWorkerOutputDTO = await dataBoard.GetProductList();
                var data = ReportOfWorkerOutputDTO.FirstOrDefault();
                ProductCount = data.ProjectCount;
                OrderCount = data.OrderCount;
                WorkCount= data.WorkCount;
            }

         );
        
        }
        public override async Task OnNavigatedToAsync(NavigationContext navigationContext = null)
        {
            await SetBusyAsync(async () =>
            {
                await GetExecutes();
                await GetProductList();
            });
        }
    }

}
